<?php

/**
 * A helper for dealing with URLs and links.
 * @package Nina
 * @author Chris Corbyn, John Main
 */
class Nina_Links
{
	/**
	 * Figure out from the registry if we want a secure connection
	 * @return string
	 */
	public static function getHttp() {
		return Nina_Registry::getInstance()->get('security.https') ? 'https' : 'http';
	}
	/**
	 * Get the URL root
	 * @return string
	 */
	public static function getUrlRoot() {
		$url_root = defined('URL_ROOT') ? URL_ROOT : $_SERVER["SERVER_NAME"];
		return self::getHttp() . '://' . $url_root;
	}

	/**
	 * Create a URL based on the rules in the routing system.
	 * @param string $controller Controller name
	 * @param string [$action='index'] Action name
	 * @param string [$id='' ]Page ID
	 * @param array [$extra=array()] Extra parameters as name => value
	 * @param string [$anchor=''] Anchor
	 * @return string
	 */
	public static function url($controller, $action = 'index', $id='', $extra=array(), $anchor='') {
	
		$url_root = defined('URL_ROOT') ? URL_ROOT : $_SERVER["SERVER_NAME"];
		
		$extras = array();
		foreach ($extra as $key => $val) {
			if(is_array($val)) {
				foreach($val as $sub_val) {
					$extras[] = $key.'[]='.$sub_val;
				}
			}
			else
				$extras[] = $key.'='.$val;
		}

		if(defined('PRETTY_URLS') && PRETTY_URLS) {
		
			$url = self::getUrlRoot().'/'.$controller.'/'.$action;
			
			if($id) $url .= '/'.$id;

			if($extras)
				$url .= '?'.implode('&amp;', $extras);
		}
		else {
			$url = self::getUrlRoot().'/index.php?controller='.$controller.'&amp;action='.$action.'&amp;id='.$id;

			if($extras)
				$url .= '&amp;'.implode('&amp;', $extras);
		}
		
		if($anchor) $url .='#'.$anchor;

		return $url;
	}

	/**
	 * Create a hyperlink tag from a URL and some text.
	 * @param string $name Link name
	 * @param string $controller Controller name
	 * @param string [$action='index'] Action name
	 * @param string [$id='' ]Page ID
	 * @param array [$extra=array()] Extra parameters as name => value
	 * @param string [$anchor=''] Anchor
	 * @param string [$class=''] CSS class
	 * @param string [$target=''] Target
	 * @param string [$title=''] Title Text
	 * @param string [$rel=''] Rel data
	 * @return string
	 */

	public static function hyperlink($name, $controller, $action = 'index', $id='', $extra=array(), $anchor='', $class='', $target='', $title='', $rel='') {
		$link = '<a href="'.Nina_Links::url($controller, $action, $id, $extra, $anchor).'"';
		
		if($class)
			$link .= ' class="'.$class.'"';
		
		if($title)
			$link .= ' title="'.$title.'"';
		
		if($rel)
			$link .= ' rel="'.$rel.'"';
		
		if($target)
			$link .= ' target="'.$target.'"';
		
		$link .= '>'.$name.'</a>';
		
		return $link;
	}

	/**
	 * Create an image tag from a URL and some text.
	 * @param string $path Path
	 * @param string [$class=''] CSS class
	 * @param string [$alt=''] Alt text
	 * @param string [$rel=''] Rel data
	 * @return string
	 */

	public static function image($path, $class = '', $alt = '', $rel = '') {
		$link = '<img src="'.$path.'"';
		
		if($class)
			$link .= ' class="'.$class.'"';
		
		if($alt)
			$link .= ' alt="'.$alt.'"';
		
		if($rel)
			$link .= ' rel="'.$rel.'"';
		
		$link .= '/>';
		
		return $link;
	}

	/**
	 * Create a button to load a page.
	 * @param string $name Button name
	 * @param string $controller Controller name
	 * @param string [$action='index'] Action name
	 * @param string [$id='' ]Page ID
	 * @param array [$extra=array()] Extra parameters as name => value
	 * @param string [$anchor=''] Anchor
	 * @param string [$class=''] CSS class
	 * @param string [$title=''] Title Text
	 * @return string
	 */
	public static function button_to($name, $controller, $action, $id='', $extra=array(), $anchor = '', $class='', $title = '') {
		$onclick = "window.location.href='".Nina_Links::url($controller, $action, $id, $extra, $anchor)."';";

		return self::button_to_function($name, $onclick, $class, $title);
	}

 	/**
	 * Create a button to run a function.
	 * @param string $name Button name
	 * @param string $function Javascript function
	 * @param string [$class=''] CSS class
	 * @param string [$title=''] Title Text
	 * @return string
	 */
	public static function button_to_function($name, $function, $class='', $title='') {
		$link = '<input type="button" value="'.$name.'" onclick="javascript:'.$function.'"';
	
		if($class)
			$link .= ' class="'.$class.'"';
			
		if($title)
			$link .= ' title="'.$title.'"';
		
		$link .= '/>';
		
		return $link;
	}

	/**
	 * Create a link to run a function.
	 * @param string $name link name
	 * @param string $function Javascript function
	 * @param string [$class=''] CSS class
	 * @param string [$title=''] Title Text
	 * @return string
	 */
	public static function link_to_function($name, $function, $class='', $title = '') {
		$link = '<a href="#" onclick="javascript:'.$function.'"';
	
		if($class)
			$link .= ' class="'.$class.'"';
			
		if($title)
			$link .= ' title="'.$title.'"';
		
		$link .= '>'.$name.'</a>';
		
		return $link;
	}

	/**
	 * Create an opening form tag
	 * @param string $action Action 
	 * @param string [$method='post'] Method
	 * @param string [$name=''] Name
	 * @param string [$class=''] Class
	 * @param string [$encoding=''] Encoding
	 * @param string [$target=''] Target
	 * @return string
	 */
	public static function form($action, $method = "post", $name = '', $class = '', $encoding = '', $target = '') {
		if(!$name)
			$name = substr(md5(microtime()), 0, 8);

		if(!$encoding)
			$encoding = 'application/x-www-form-urlencoded';

		$link = '<form action="'.$action.'" method="'.$method.'" name="'.$name.'" id="'.$name.'" enctype="'.$encoding.'"';
		
		if($class)
			$link .= ' class="'.$class.'"';

		if($target)
			$link .= ' target="'.$target.'"';
		
		$link .= '>' . PHP_EOL;

		return $link;
	}
	
	/**
	 * Create a populated select tag
	 * @param string $id ID
	 * @param array $options Options
	 * @param string|array $selected Selected option
	 * @param boolean|string $include_blank Include blank element
	 * @param string $class Class
	 * @param boolean $multiple Multiple select box
	 * @return string
	 */
	public static function select_tag($id, $options, $selected = '', $include_blank = false, $class = '', $multiple = false) {
		$element = '<select id="'.$id.'" name="'.$id.'"';
		
		if($class)
			$element .= ' class="'.$class.'"';
		
		if($multiple)
			$element .= ' multiple="multiple"';

		$element .= '>';
		
		if($include_blank) {
			if(is_string($include_blank))
				$select_text = $include_blank;
			else
				$select_text = 'Select...';
			
			$element .= '<option value="">'.$select_text.'</option>';
		}

		foreach($options as $option_key => $option_value) {
			
			if(is_array($option_value)) {
				$element .= '<option value="'.$option_value[1].'"';
				
				if(($multiple && is_array($selected) && in_array($option_value[1], $selected)) || $option_value[1] == $selected)
					$element .= ' selected="selected"';
					
				$element .= '>'.$option_value[0].'</option>';
			}
			else {
				$element .= '<option value="'.$option_key.'"';
				
				if(($multiple && is_array($selected) && in_array($option_key, $selected)) || $option_key == $selected)
					$element .= ' selected="selected"';
					
				$element .= '>'.$option_value.'</option>';
			}
		}
		 
		$element .= '</select>';
		 
		return $element;
	}
	
	/**
	 * Create a populated select group tag
	 * @param string $id ID
	 * @param array $option_groups Option groups
	 * @param string|array $selected Selected option
	 * @param boolean|string $include_blank Include blank element
	 * @param string $class Class
	 * @param boolean $multiple Multiple select box
	 * @return string
	 */
	public static function select_group_tag($id, $option_groups, $selected = '', $include_blank = false, $class = '', $multiple = false) {
		$element = '<select id="'.$id.'" name="'.$id.'"';
		
		if($class)
			$element .= ' class="'.$class.'"';
		
		if($multiple)
			$element .= ' multiple="multiple"';

		$element .= '>';
		
		if($include_blank) {
			if(is_string($include_blank))
				$select_text = $include_blank;
			else
				$select_text = 'Select...';
			
			$element .= '<option value="">'.$select_text.'</option>';
		}
		 
		foreach($option_groups as $group_name => $options) {
			$element .= '<optgroup label="'.$group_name.'">';
		 
			foreach($options as $option_key => $option_value) {
			
				if(is_array($option_value)) {
					$element .= '<option value="'.$option_value[1].'"';
					
					if(($multiple && is_array($selected) && in_array($option_value[1], $selected)) || $option_value[1] == $selected)
						$element .= ' selected="selected"';
						
					$element .= '>'.$option_value[0].'</option>';
				}
				else {
					$element .= '<option value="'.$option_key.'"';
					
					if(($multiple && is_array($selected) && in_array($option_key, $selected)) || $option_key == $selected)
						$element .= ' selected="selected"';
						
					$element .= '>'.$option_value.'</option>';
				}
			}

			$element .= '</optgroup>';
		}
		 
		$element .= '</select>';
		 
		return $element;
	}
}
